﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.CardMaking.CardOutStorage
{
    /// <summary>
    /// 根据箱号获取白卡入库明细
    /// </summary>
    public class GetStorageDetail : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strRKId = context.Request.QueryString["RKId"];//入库单ID
            string strCardNo = context.Request.QueryString["CardNo"];//盒号
            if (strRKId.Trim().Length>0|| strRKId.Trim().Length > 0)
            {
                string strWhere = " RKID='" + strRKId + "' and CardHNo='" + strCardNo + "'"; //卡流水查询条件
                //DataTable dt = new DAL.CardArrival.Storage_Detailed_CardData().GetList(strWhere, "*");
                //根据出库明细和卡流水明细表，过滤已出库卡号信息
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select ID,BOXNO,(select CardNo from(select * from cardrecord ");
                strSql.Append("where BKCHUKUSJ is null and BoxNo is not null ");
                strSql.Append("and " + strWhere);
                strSql.Append("order by cardno) where rownum=1) as STARTNO,ENDNO,CARDNO,");
                strSql.Append("(select count(*) from cardrecord where BKCHUKUSJ is null and BoxNo is not null ");
                strSql.Append("and " + strWhere + ") as ACTUALNUMBER,");
                strSql.Append("MISSINGCARDNO,CHECKID,(select (max(CardNo)- min(CardNo)+1) from cardrecord ");
                strSql.Append("where BKCHUKUSJ is null and BoxNo is not null ");
                strSql.Append("and " + strWhere + ")as DESERVEDNUMBER ");
                strSql.Append("from Storage_Detailed_Card where Cardno='" + strCardNo + "' and checkid='" + strRKId + "'");
                DataTable dt = DAL.DbHelperSQL.GetQueryResult(strSql.ToString());
                string retContent = "";
                if (dt != null && dt.Rows.Count > 0)
                {
                    string missingCardNo = "";//缺失卡号
                    //判断缺卡卡号是否在开始和结束卡号范围内
                    string sql = " select Fun_GetMisCard(" + dt.Rows[0]["STARTNO"].ToString() + "," + dt.Rows[0]["ENDNO"].ToString() + ") MCard FROM dual ";
                    DataTable dtMCard = CardLogistics.DAL.DbHelperSQL.GetQueryResult(sql);
                    if (dtMCard != null && dtMCard.Rows.Count > 0)
                        missingCardNo = dtMCard.Rows[0][0].ToString();
                   
                    //Id明细ID;CARDNO盒号;STARTNO起始卡号;ENDNO结束卡号;DESERVEDNUMBER应有数量/张;ACTUALNUMBER实际数量/张;MISSINGCARDNO缺卡卡号;BoxNo箱号
                    //retContent += dt.Rows[0]["checkId"].ToString() + ";" + dt.Rows[0]["CARDNO"].ToString() + ";" + dt.Rows[0]["STARTNO"].ToString() + ";" + dt.Rows[0]["ENDNO"].ToString() + ";" + dt.Rows[0]["DESERVEDNUMBER"].ToString() + ";" + dt.Rows[0]["ACTUALNUMBER"].ToString() + ";" + dt.Rows[0]["MISSINGCARDNO"].ToString() + ";" + dt.Rows[0]["BoxNo"].ToString();
                    retContent += dt.Rows[0]["checkId"].ToString() +","+dt.Rows[0]["CARDNO"].ToString()+ ";" + dt.Rows[0]["CARDNO"].ToString() + ";" + dt.Rows[0]["STARTNO"].ToString() + ";" + dt.Rows[0]["ENDNO"].ToString() + ";" + dt.Rows[0]["DESERVEDNUMBER"].ToString() + ";" + dt.Rows[0]["ACTUALNUMBER"].ToString() + ";" + missingCardNo.TrimEnd(',') + ";" + dt.Rows[0]["BoxNo"].ToString();
                }
                context.Response.Write(retContent);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}